﻿using Cart.NguoiDungSrv;
using Cart.SanPhamSrv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Cart
{
    public partial class DanhSachSanPham : VdcInc.vdcAJAXPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsCallback)
            {
                #region check quyen
                var user = Session["nsd"] as NguoiDung;
                if (user != null)
                {
                    if (user.Quyen != 1) ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Không có quyền truy cập !'); window.location='" + Request.ApplicationPath + "TrangChu.aspx';", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Redit", "alert('Không có quyền truy cập !'); window.location='" + Request.ApplicationPath + "TrangChu.aspx';", true);
                }

                #endregion check quyen
                using (var ndService = new SanPhamSrv.SanPhamServiceClient())
                {
                    var lstLSP = ndService.DanhSachLoaiSanPham();
                    ddlLoaiSanPham.DataSource = lstLSP;
                    ddlLoaiSanPham.DataTextField = "Ten";
                    ddlLoaiSanPham.DataValueField = "Id";
                    ddlLoaiSanPham.DataBind();
                    if (lstLSP != null)
                    {
                        var lspfirst = new LoaiSanPham();
                        lspfirst = lstLSP.ElementAt(0);
                        var lstDS = ndService.DanhSachSanPham(lspfirst.Id);
                        Session["IdLoaiSanPham"] = lspfirst.Id;
                        lvDanhSach.DataSource = lstDS;
                        lvDanhSach.DataBind();
                    }

                    //var lstDS = ndService.DanhSachSanPham(0);
                    //lvDanhSach.DataSource = lstDS;
                    //lvDanhSach.DataBind();
                }
            }
        }
        public void vThayDoiLoai(string strLoaiId)
        {
            if (strLoaiId != "")
            {
                using (var ndService = new SanPhamSrv.SanPhamServiceClient())
                {
                    var lstDS = ndService.DanhSachSanPham(Convert.ToInt64(strLoaiId));
                    lvDanhSach.DataSource = lstDS;
                    lvDanhSach.DataBind();
                    Session["IdLoaiSanPham"] = Convert.ToInt64(strLoaiId);
                    UpdatePanel("cbpGrid");
                }
            }
        }
        public string vXoaSanPham(string strSanPhamId)
        {
            using (var ndService = new SanPhamSrv.SanPhamServiceClient())
            {
                var sanphammodel = new SanPham();
                sanphammodel.Id = Convert.ToInt64(strSanPhamId);
                var result = ndService.CapNhatSanPham(sanphammodel);
                return "Xóa " + result;
            }
        }
    }
}